package com.example.test2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.test2.R;
import com.example.test2.adapter.BuyAdapter;
import com.example.test2.adapter.SearchClothesAdapter;
import com.example.test2.databinding.ActivitySearchBinding;
import com.example.test2.db.control.BuyDbController;
import com.example.test2.db.control.ClothesDbController;
import com.example.test2.db.entity.Buy;
import com.example.test2.db.entity.Clothes;
import com.example.test2.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

// TODO: 2024/6/02 搜索
public class SearchActivity extends BaseActivity<ActivitySearchBinding> implements View.OnClickListener {

    private ClothesDbController clothesDbController;
    private BuyDbController dbPurchaseController;
    private List<Clothes> list = new ArrayList<>();
    private List<Buy> list2 = new ArrayList<>();
    private SearchClothesAdapter searchClothesAdapter;
    private BuyAdapter purchaseAdapter;
    private int type;
    @Override
    protected void initData() {
        super.initData();
        clothesDbController= ClothesDbController.getInstance(this);
        dbPurchaseController= BuyDbController.getInstance(this);
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().tvSearch.setOnClickListener(this);
        getBinding().ivBack.setOnClickListener(this);
        getBinding().indicatorView.setOnIndicatorChangedListener((oldSelectedIndex, newSelectedIndex) -> {
            type=newSelectedIndex;
        });
    }

    private void inAdapter(int type) {

        if (list.size()>0||list2.size()>0){
            getBinding().ivShow.setVisibility(View.GONE);
        }

        if (type==1){
            getBinding().rvData.setLayoutManager(new LinearLayoutManager(this));
            if (searchClothesAdapter==null){
                searchClothesAdapter=new SearchClothesAdapter(this,list);
                getBinding().rvData.setAdapter(searchClothesAdapter);
            }else {
                searchClothesAdapter.setList(list);
                searchClothesAdapter.notifyDataSetChanged();
            }
            searchClothesAdapter.setListener(pos -> {
                Intent intent=new Intent(this, ClothesActivity.class);
                intent.putExtra("clothes",list.get(pos));
                startActivity(intent);
            });
        }else {
            getBinding().rvData.setLayoutManager(new LinearLayoutManager(this));
            if (purchaseAdapter==null){
                purchaseAdapter=new BuyAdapter(this,list2);
                getBinding().rvData.setAdapter(purchaseAdapter);
            }else {
                purchaseAdapter.setList(list2);
                purchaseAdapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected ActivitySearchBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivitySearchBinding.inflate(layoutInflater);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_search:
                search();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void search() {
        String name = getBinding().etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.getInstance().show(this, "请输入商品名称");
        }else {
            if (type==0){
                list = clothesDbController.searchByName(name);
                inAdapter(1);
            }else if (type==1){
                list2 = dbPurchaseController.searchByName(name);
                inAdapter(0);
            }else {
                list = clothesDbController.searchByShopName(name);
                inAdapter(1);
            }

        }
    }
}